package person.bing.fbcloud.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import person.bing.fbcloud.api.entity.Post;
import person.bing.fbcloud.api.vo.HomePagePostVo;
import person.bing.fbcloud.common.core.exception.CheckedException;
import person.bing.fbcloud.mapper.PostMapper;
import person.bing.fbcloud.service.PostService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 帖子表 服务实现类
 * </p>
 *
 * @author mengfb
 * @since 2019-06-24
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {

    /**
     * 发帖
     * @param userCode
     * @param title
     * @param text
     */
    @Override
    public void addPost(String userCode,String categoryKey, String title, String text) {
        LocalDateTime now = LocalDateTime.now();
        Post post = new Post();
        post.setId(IdUtil.simpleUUID());
        post.setCategoryKey(categoryKey);
        post.setTitle(title);
        post.setText(text);
        post.setUserCode(userCode);
        post.setCreateTime(now);
        post.setUdpateTime(now);
        this.save(post);
    }

    /**
     * 根据categoryKey获取帖子列表
     * @param categoryKey
     * @return
     */
    @Override
    public List<HomePagePostVo> getPost(String categoryKey) {
        List<HomePagePostVo> homePagePostVoList = new ArrayList<>();
        List<Post> postList = this.list(Wrappers.<Post>query().lambda()
                .select(Post::getId,Post::getTitle,Post::getUserCode,Post::getText)
                .eq(Post::getCategoryKey, categoryKey));
        postList.stream().forEach(post -> {
            HomePagePostVo homePagePostVo = new HomePagePostVo();
            BeanUtil.copyProperties(post,homePagePostVo);
//            homePagePostVo.setUserFace();
            homePagePostVoList.add(homePagePostVo);
        });
        return homePagePostVoList;
    }

    /**
     * 根据帖子id获取详情
     * @param postId
     * @return
     */
    @Override
    public Post getPostDetails(String postId) {
        Post post = this.getById(postId);
        return post;
    }


//    @SneakyThrows
    @Override
    public void test(){
//        try {
        throw new CheckedException("试下throws");
//        }catch (Exception e){
//            System.out.println("--我是test方法--");
//            System.out.println(e.getMessage());
//        }
    }

}
